<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Categories extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'));
        $this->data['controller'] = 'categories';         
        $this->load->model('Mcategories');
    }
    
    
    public function product()
    {
        $this->data['data'] = $this->Mcategories->getCategoriesDB($this->data['baseurl'],1,'categories-product');
        $this->data['action'] = 'product';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editcateproduct()
    {
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nameCate', 'Tên danh mục', 'required');
        $this->form_validation->set_rules('nameCateSeo', 'Tên danh mục SEO', 'required');
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mcategories->getCategories($this->data['id']);
        $this->data['parent'] = $this->Mcategories->getParent($this->data['data']->parent,1);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mcategories->editCateDb($this->data['id'],$img,1,$this->data['baseurl']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa danh mục thành công");window.location.href="'.$this->data['baseurl'].'manage/categories-product.html";</script>');
            }
        }
        $this->data['action'] = 'editcateproduct';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addcateproduct()
    {
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nameCate', 'Tên danh mục', 'required');
        $this->form_validation->set_rules('nameCateSeo', 'Tên danh mục SEO', 'required');
        $this->data['parent'] = $this->Mcategories->getParent('', 1);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mcategories->addCateDb($img,1,$this->data['baseurl']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm danh mục thành công");window.location.href="'.$this->data['baseurl'].'manage/categories-product.html";</script>');
            }
        }
        $this->data['action'] = 'addcateproduct';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deletemenuproduct()
    {
        $run = $this->Mcategories->deletemenuproductDB();
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa danh mục thành công");window.location.href="'.$this->data['baseurl'].'manage/categories-product.html";</script>');
        }
        
    }
    
    
    /**
     * 
     * @Danh mục content
     * 
     */
    
    
    public function content()
    {
        $this->data['data'] = $this->Mcategories->getCategoriesDB($this->data['baseurl'],2,'categories-content');
        $this->data['action'] = 'content';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editcatecontent()
    {
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nameCate', 'Tên danh mục', 'required');
        $this->form_validation->set_rules('nameCateSeo', 'Tên danh mục SEO', 'required');
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mcategories->getCategories($this->data['id']);
        $this->data['parent'] = $this->Mcategories->getParent($this->data['data']->parent,2);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mcategories->editCateDb($this->data['id'],$img,2,$this->data['baseurl']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa danh mục thành công");window.location.href="'.$this->data['baseurl'].'manage/categories-content.html";</script>');
            }
        }
        $this->data['action'] = 'editcatecontent';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addcatecontent()
    {
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nameCate', 'Tên danh mục', 'required');
        $this->form_validation->set_rules('nameCateSeo', 'Tên danh mục SEO', 'required');
        $this->data['parent'] = $this->Mcategories->getParent('', 2);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mcategories->addCateDb($img,2,$this->data['baseurl']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm danh mục thành công");window.location.href="'.$this->data['baseurl'].'manage/categories-content.html";</script>');
            }
        }
        $this->data['action'] = 'addcatecontent';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deletemenucontent()
    {
        $run = $this->Mcategories->deletemenuproductDB();
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa danh mục thành công");window.location.href="'.$this->data['baseurl'].'manage/categories-content.html";</script>');
        }
        
    }
    
    /**
     * 
     * @Danh mục khác
     * 
     */
    
    
    public function other()
    {
        $this->data['data'] = $this->Mcategories->getCategoriesDB($this->data['baseurl'],3,'categories-other');
        $this->data['action'] = 'other';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editcateother()
    {
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nameCate', 'Tên danh mục', 'required');
        $this->form_validation->set_rules('nameCateSeo', 'Tên danh mục SEO', 'required');
        
        $this->data['id'] = $this->getParamInt('id');
        $this->data['data'] = $this->Mcategories->getCategories($this->data['id']);
        $this->data['parent'] = $this->Mcategories->getParent($this->data['data']->parent,3);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mcategories->editCateDb($this->data['id'],$img,3,$this->data['baseurl']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa danh mục thành công");window.location.href="'.$this->data['baseurl'].'manage/categories-other.html";</script>');
            }
        }
        $this->data['action'] = 'editcateother';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addcateother()
    {
        
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('nameCate', 'Tên danh mục', 'required');
        $this->form_validation->set_rules('nameCateSeo', 'Tên danh mục SEO', 'required');
        $this->data['parent'] = $this->Mcategories->getParent('', 3);
        
        if($this->form_validation->run() === TRUE)
        {
            $dataimage = $this->getParamString('img_hidden');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'nhatnv-'.$timenow.'.png';
                $paths = PROJECT_PATH.'/public/upload/images/';
                $exp = explode("[removed]",$dataimage);
                $this->savefile($exp[1],$paths,$nameimagess);
                $img = 'public/upload/images/'.$nameimagess;
            }else{
                $img = '';
            }
        
            $run = $this->Mcategories->addCateDb($img,3,$this->data['baseurl']);
            if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm danh mục thành công");window.location.href="'.$this->data['baseurl'].'manage/categories-other.html";</script>');
            }
        }
        $this->data['action'] = 'addcateother';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function deletemenuother()
    {
        $run = $this->Mcategories->deletemenuotherDB();
        if($run == 200)
        {
            die('<meta charset="utf-8"><script>alert("Xóa danh mục thành công");window.location.href="'.$this->data['baseurl'].'manage/categories-other.html";</script>');
        }
        
    }
    
    public function sortcate()
    {
        $this->data['data'] = $this->Mcategories->getNamecate();
        $this->data['action'] = 'sortcate';
        $this->load->view('temp/layout', $this->data);
    } 
    
    public function savesort()
    {
        $id_list = $this->getParamArray('id');
        $this->Mcategories->sortsaveDB($id_list);
    }
    
}
